package com.oocl.tm.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;

import com.oocl.tm.log4j.Log4j;

public class DBUtil {

	private static BasicDataSource ds;
	private static Properties pro;
	private static InputStream in;
	
	
	static{
		// init the database connection information
		try {
			ds = new BasicDataSource();
			in = new BufferedInputStream(new FileInputStream("db.properties"));
			pro = new Properties();
			pro.load(in);
			ds.setDriverClassName(pro.getProperty("driverClass"));
			ds.setUsername(pro.getProperty("userName"));
			ds.setPassword(pro.getProperty("pwd"));
			ds.setUrl(pro.getProperty("url"));
			
		} catch (IOException e) {
			Log4j.getLogger().fatal(e, e.fillInStackTrace());
		}
		
		//load all clients to hashmap
		createConnectionWithDataSource();
	}
	
	public static Connection createConnectionWithDataSource(){
		Connection con = null;
		try {
			con = ds.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}
	
	/**
	 * 
	 * @param con
	 * @param pst
	 * @param rs
	 */
	public static void close(Connection con, PreparedStatement pst, ResultSet rs){
		try {
			if(rs!=null)
				rs.close();
			if(pst!=null)
				pst.close();
			if(con!=null)
				con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static HashMap<String, Boolean> loadClients() {
			
			return null;
	}
	
}


